package storageplugin

import (
	"fmt"
	"github.com/emicklei/go-restful"
	"kubeInstall/pkg/logger"
	"kubeInstall/server/handlers"
	nfsmodels "kubeInstall/server/models/storageplugin"
	"kubeInstall/server/services/storageplugin"
	"kubeInstall/utils/validate"
	"net/http"
)

type nfsHandler struct {
	handlers.BaseHandler
}

func NewNfsHandler() handlers.IBaseHandler {
	return new(nfsHandler)
}

func (h *nfsHandler) AddToRoute(ws *restful.WebService) {

	ws.Route(ws.GET("/storage/nfs/{nfs_id}").To(h.getStoragePluginNFS))
	// add nfs
	ws.Route(ws.POST("/storage/nfs").To(h.addStoragePluginNFS))
	// delete nfs
	ws.Route(ws.DELETE("/storage/nfs/{nfs_id}").To(h.deleteStoragePluginNFS))
	// list nfs
	ws.Route(ws.GET("/storage/nfs").To(h.listStoragePluginNFS))
	// update nfs
	ws.Route(ws.PUT("/storage/nfs/{nfs_id}").To(h.updateStoragePluginNFS))
	logger.Info("add storage module successfully")
}

func (n *nfsHandler) addStoragePluginNFS(r *restful.Request, resp *restful.Response) {

	var req nfsmodels.StorageNFS
	if err := r.ReadEntity(&req); err != nil {
		n.RenderError(resp, fmt.Sprintf("read nfs from http body failed,error is :%v", err), http.StatusBadRequest)
		return
	}

	if err := validate.ValidateIP(req.NFSServerIp); err != nil {
		n.RenderError(resp, fmt.Sprintf("Validate IP failed . error is :%v", err), http.StatusBadRequest)
		return
	}

	if storageplugin.CheckNfsServerIpIsExist(req.NFSServerIp) {
		n.RenderError(resp, fmt.Sprintf(" NFS Server Ip : %s already exsist", req.NFSServerIp), http.StatusBadRequest)
		return
	}

	//

	req.Status = storageplugin.CheckNfsServerStatus(req.NFSServerIp)

	result, err := storageplugin.AddStoragePluginNFS(&req)

	if err != nil {
		n.RenderError(resp, fmt.Sprintf("create nfs failed! error is :%v", err), http.StatusBadRequest)
		return
	}
	n.RenderResp(resp, result, http.StatusOK)

}

func (n *nfsHandler) getStoragePluginNFS(r *restful.Request, resp *restful.Response) {

	nfsId := r.PathParameter("nfs_id")
	if nfsId == "" {
		n.RenderError(resp, fmt.Sprintf("get Nfs id from http query failed!"), http.StatusBadRequest)
		return
	}
	result, err := storageplugin.GetStoragePluginNFS(nfsId)

	if err != nil {
		n.RenderError(resp, fmt.Sprintf("get nfs failed! error is: %v", err), http.StatusBadRequest)
		return
	}

	n.RenderResp(resp, result, http.StatusOK)

}

func (n *nfsHandler) listStoragePluginNFS(r *restful.Request, resp *restful.Response) {

	result, err := storageplugin.ListStoragePluginNFS()
	if err != nil {
		n.RenderError(resp, fmt.Sprintf("list nfs failed! error is %v", err), http.StatusInternalServerError)
		return
	}

	n.RenderResp(resp, result, http.StatusOK)

}

func (n *nfsHandler) deleteStoragePluginNFS(r *restful.Request, resp *restful.Response) {

	nfsId := r.PathParameter("nfs_id")
	if nfsId == "" {
		n.RenderError(resp, fmt.Sprintf("get Nfs id from http query failed!"), http.StatusBadRequest)
		return
	}

	if err := storageplugin.DeleteStoragePluginNFS(nfsId); err != nil {
		n.RenderError(resp, fmt.Sprintf("delete nfs failed! error is  %v", err), http.StatusInternalServerError)
		return
	}

	result := handlers.SuccessResp{
		StatusCode: http.StatusOK,
		Message:    fmt.Sprintf("Delete %s Success", nfsId),
	}

	n.RenderResp(resp, result, http.StatusOK)
}

func (n *nfsHandler) updateStoragePluginNFS(r *restful.Request, resp *restful.Response) {
	//仅能够修改名字
	var nfs nfsmodels.StorageNFS
	nfsId := r.PathParameter("nfs_id")
	if nfsId == "" {
		n.RenderError(resp, fmt.Sprintf("get Nfs id from http query failed!"), http.StatusBadRequest)
		return
	}

	if err := r.ReadEntity(&nfs); err != nil {
		n.RenderError(resp, fmt.Sprintf("read nfs from http body failed,error is :%v", err), http.StatusBadRequest)
		return
	}

	if storageplugin.CheckNfsIdIsUsed(nfsId) {
		n.RenderError(resp, fmt.Sprintf("update nfs failed! nfsId is used %s", nfsId), http.StatusBadRequest)
		return
	}

	if err := storageplugin.UpdateStoragePluginNFS(nfsId, &nfs); err != nil {
		n.RenderError(resp, fmt.Sprintf("update nfs failed! error is  %v", err), http.StatusBadRequest)
		return
	}

	result, err := storageplugin.GetStoragePluginNFS(nfsId)

	if err != nil {
		n.RenderError(resp, fmt.Sprintf("get nfs failed! error is: %v", err), http.StatusBadRequest)
		return
	}

	n.RenderResp(resp, result, http.StatusOK)

}
